// PendingIssueController.java
package com.bolingcavalry.crm.pendingissue.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.base.PageResult;
import com.base.ResponseResult;
import com.bolingcavalry.crm.pendingissue.service.PendingIssueService;
import com.bolingcavalry.crm.security.utils.SecurityUtils;
import com.bolingcavalry.crm.utils.AUtils;
import com.entity.pendingissue.PendingIssue;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @Description: 待处理问题控制器
 */
@RestController
@RequestMapping("/pending-issue")
@Slf4j
public class PendingIssueController {

    @Autowired
    private PendingIssueService pendingIssueService;

    /**
     * 根据id查询待处理问题
     * @param id
     * @return
     */
    @GetMapping("/get/{id}")
    public ResponseResult<PendingIssue> getById(@PathVariable Long id) {
        return ResponseResult.success(pendingIssueService.getById(id));
    }
    @Autowired
    private AUtils aUtils;
    /**
     * 创建待处理问题
     * @param pendingIssue
     * @return
     */
    @PostMapping("/add")
    public ResponseResult create(@RequestBody PendingIssue pendingIssue) {
        if(pendingIssue==null){
            return ResponseResult.error("参数不能为空");
        }
        if(pendingIssue.getIssueDetails()== null|| pendingIssue.getIssueDetails().trim().length()==0){
            return ResponseResult.error("问题详细不能为空");
        }
        if(pendingIssue.getServiceLocation()== null|| pendingIssue.getServiceLocation().trim().length()==0){
            return ResponseResult.error("服务位置不能为空");
        }
        if(pendingIssue.getIssueCategory()== null|| pendingIssue.getIssueCategory().trim().length()==0){
            return ResponseResult.error("问题归类不能为空");
        }
        if(pendingIssue.getCustomerCompanyName()== null|| pendingIssue.getCustomerCompanyName().trim().length()==0){
            return ResponseResult.error("客户公司名称不能为空");
        }
        pendingIssue.setCreateTime(LocalDateTime.now());
        pendingIssue.setUpdateTime(LocalDateTime.now());
        pendingIssue.setCreatedBy(aUtils.getNickNameByuserName(SecurityUtils.getCurrentUsername()));
        if(pendingIssueService.save(pendingIssue)){
            return ResponseResult.success("待处理问题创建成功");
        }
        return ResponseResult.error("待处理问题创建失败");
    }

    /**
     * 更新待处理问题
     * @param pendingIssue
     * @return
     */
    @PostMapping("/update")
    public ResponseResult update(@RequestBody PendingIssue pendingIssue) {
        if(pendingIssue==null||pendingIssue.getId()==null){
            return ResponseResult.error("参数不能为空");
        }
        LambdaQueryWrapper<PendingIssue> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(PendingIssue::getId, pendingIssue.getId());
        PendingIssue pendingIssueInDb = pendingIssueService.getOne(queryWrapper);
        if(pendingIssueInDb==null){
            return ResponseResult.error("待处理问题不存在");
        }
        if(pendingIssue.getIssueCategory()!= null){
            pendingIssueInDb.setIssueCategory(pendingIssue.getIssueCategory());
        }
        if(pendingIssue.getIssueDetails()!= null){
            pendingIssueInDb.setIssueDetails(pendingIssue.getIssueDetails());
        }
        if(pendingIssue.getServiceLocation()!= null){
            pendingIssueInDb.setServiceLocation(pendingIssue.getServiceLocation());
        }
        if(pendingIssue.getCustomerCompanyName()!= null){
            pendingIssueInDb.setCustomerCompanyName(pendingIssue.getCustomerCompanyName());
        }
        if(pendingIssue.getContactPerson()!= null){
            pendingIssueInDb.setContactPerson(pendingIssue.getContactPerson());
        }
        if(pendingIssue.getContactPhone()!= null){
            pendingIssueInDb.setContactPhone(pendingIssue.getContactPhone());
        }
        if(!pendingIssueInDb.getCreatedBy().equals(aUtils.getNickNameByuserName(SecurityUtils.getCurrentUsername()))
                &&!aUtils.getRoleByUserName(SecurityUtils.getCurrentUsername()).contains("ADMIN")){
            return ResponseResult.error("只有创建者或特定角色可以更新");
        }
        pendingIssueInDb.setUpdateTime(LocalDateTime.now());
        if(pendingIssueService.updateById(pendingIssueInDb)){
            return ResponseResult.success("待处理问题更新成功");
        }
        return ResponseResult.error("待处理问题更新失败");
    }

    /**
     * 根据id删除待处理问题
     * @param id
     * @return
     */
    @DeleteMapping("/delete/{id}")
    public ResponseResult delete(@PathVariable Long id) {
       if(pendingIssueService.removeById(id)){
           return ResponseResult.success("待处理问题删除成功");
       }
       return ResponseResult.error("待处理问题删除失败");
    }

    /**
     * 分页条件查询待处理问题
     */
    @GetMapping("/list")
    public ResponseResult<PendingIssue> list(
            @RequestParam(value = "pageNum", required = false, defaultValue = "1") Integer pageNum,
            @RequestParam(value = "pageSize", required = false,defaultValue = "100") Integer pageSize,
            @RequestParam(value = "customerCompanyName", required = false) String customerCompanyName, // 客户公司名称
            @RequestParam(value = "serviceLocation", required = false) String serviceLocation, // 服务位置
            @RequestParam(value = "issueCategory", required = false) String issueCategory // 问题归类
    ) {
        IPage<PendingIssue> page = pendingIssueService.getByConditions(pageNum, pageSize, customerCompanyName, serviceLocation, issueCategory);
        PageResult<PendingIssue> result = new PageResult<>();
        result.setTotal(page.getTotal());
        result.setPages((int) page.getPages());
        result.setCurrent((int) page.getCurrent());
        result.setSize((int) page.getSize());
        result.setRecords(page.getRecords());
        return ResponseResult.success("查询成功", result);
    }
}
